package com.foyekoo.project001.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.foyekoo.project001.R;
import com.foyekoo.project001.command.Command;
import com.foyekoo.project001.command.ReceivedCommand;
import com.foyekoo.project001.config.SpUtil;
import com.foyekoo.project001.service.BackgroundMusicService;
import com.foyekoo.project001.utils.BillUtil;
import com.foyekoo.project001.utils.ReadThread;
import com.foyekoo.project001.utils.TimeUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlayGameActivity extends BaseActivity {

    @BindView(R.id.tv_time)
    public TextView gameTime;


    @BindView(R.id.tv_voice_text)
    public TextView voiceData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playing);
        ButterKnife.bind(this);
        startService(new Intent(this,BackgroundMusicService.class));

        gameTime.setText(SpUtil.getInstance().getLocalClient().gameTime + "");
        Command.gameUILoadingSuccess();


    }




    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(dataReceiver,new IntentFilter(ReadThread.ACTION_DATA_RECIEVE));
        sendBroadcast(new Intent(BackgroundMusicService.ACTION_START));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(dataReceiver);

        sendBroadcast(new Intent(BackgroundMusicService.ACTION_STOP));
    }


    public BroadcastReceiver dataReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           byte[] data = intent.getByteArrayExtra(ReadThread.EXTRA_DATA);

           if(data[2] == 0x02){
               finish();
           }

           if (data[2] == 0x0b) { //播放音乐
             if(SpUtil.getInstance().getLocalClient().backGroundMusicIsOpen == true) {
                 String voiceText = ReceivedCommand.getPlayMusic(data);
                 voiceData.setText(voiceText);
             }
           } else if (data[2] == 0x0E){ //
             gameTime.setText(ReceivedCommand.getCurrentPlayTime(data));
           } else if (data[2] == 0x08) { //中奖物品掉落 //写到账单里
               //中奖了就直接写一个到账单里面 投币数为0
               BillUtil.insert(TimeUtil.getTimeFormat("yyyy-MM-dd"),"0","1");
           }
        }
    };


}
